package cn.chowx.myjfinal.util;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 公用工具类
 * @author zcqshine
 *
 */
public class CommonUtil {
	private static final Logger LOG = LoggerFactory.getLogger(CommonUtil.class);
	/**
	 * http MIME类型
	 * @param returnFileName
	 * @return
	 */
	public static String setContentType(String returnFileName){  
        String contentType = "application/octet-stream";  
        if (returnFileName.lastIndexOf(".") < 0)  
            return contentType;  
        returnFileName = returnFileName.toLowerCase();  
        returnFileName = returnFileName.substring(returnFileName.lastIndexOf(".")+1);  
          
        if (returnFileName.equals("html") || returnFileName.equals("htm") || returnFileName.equals("shtml")){  
            contentType = "text/html";  
        } else if (returnFileName.equals("css")){  
            contentType = "text/css";  
        } else if (returnFileName.equals("xml")){  
            contentType = "text/xml";  
        } else if (returnFileName.equals("gif")){  
            contentType = "image/gif";  
        } else if (returnFileName.equals("jpeg") || returnFileName.equals("jpg")){  
            contentType = "image/jpeg";  
        } else if (returnFileName.equals("js")){  
            contentType = "application/x-javascript";  
        } else if (returnFileName.equals("atom")){  
            contentType = "application/atom+xml";  
        } else if (returnFileName.equals("rss")){  
            contentType = "application/rss+xml";  
        } else if (returnFileName.equals("mml")){  
            contentType = "text/mathml";   
        } else if (returnFileName.equals("txt")){  
            contentType = "text/plain";  
        } else if (returnFileName.equals("jad")){  
            contentType = "text/vnd.sun.j2me.app-descriptor";  
        } else if (returnFileName.equals("wml")){  
            contentType = "text/vnd.wap.wml";  
        } else if (returnFileName.equals("htc")){  
            contentType = "text/x-component";  
        } else if (returnFileName.equals("png")){  
            contentType = "image/png";  
        } else if (returnFileName.equals("tif") || returnFileName.equals("tiff")){  
            contentType = "image/tiff";  
        } else if (returnFileName.equals("wbmp")){  
            contentType = "image/vnd.wap.wbmp";  
        } else if (returnFileName.equals("ico")){  
            contentType = "image/x-icon";  
        } else if (returnFileName.equals("jng")){  
            contentType = "image/x-jng";  
        } else if (returnFileName.equals("bmp")){  
            contentType = "image/x-ms-bmp";  
        } else if (returnFileName.equals("svg")){  
            contentType = "image/svg+xml";  
        } else if (returnFileName.equals("jar") || returnFileName.equals("var") || returnFileName.equals("ear")){  
            contentType = "application/java-archive";  
        } else if (returnFileName.equals("doc")){  
            contentType = "application/msword";  
        } else if (returnFileName.equals("pdf")){  
            contentType = "application/pdf";  
        } else if (returnFileName.equals("rtf")){  
            contentType = "application/rtf";  
        } else if (returnFileName.equals("xls")){  
            contentType = "application/vnd.ms-excel";   
        } else if (returnFileName.equals("ppt")){  
            contentType = "application/vnd.ms-powerpoint";  
        } else if (returnFileName.equals("7z")){  
            contentType = "application/x-7z-compressed";  
        } else if (returnFileName.equals("rar")){  
            contentType = "application/x-rar-compressed";  
        } else if (returnFileName.equals("swf")){  
            contentType = "application/x-shockwave-flash";  
        } else if (returnFileName.equals("rpm")){  
            contentType = "application/x-redhat-package-manager";  
        } else if (returnFileName.equals("der") || returnFileName.equals("pem") || returnFileName.equals("crt")){  
            contentType = "application/x-x509-ca-cert";  
        } else if (returnFileName.equals("xhtml")){  
            contentType = "application/xhtml+xml";  
        } else if (returnFileName.equals("zip")){  
            contentType = "application/zip";  
        } else if (returnFileName.equals("mid") || returnFileName.equals("midi") || returnFileName.equals("kar")){  
            contentType = "audio/midi";  
        } else if (returnFileName.equals("mp3")){  
            contentType = "audio/mpeg";  
        } else if (returnFileName.equals("ogg")){  
            contentType = "audio/ogg";  
        } else if (returnFileName.equals("m4a")){  
            contentType = "audio/x-m4a";  
        } else if (returnFileName.equals("ra")){  
            contentType = "audio/x-realaudio";  
        } else if (returnFileName.equals("3gpp") || returnFileName.equals("3gp")){  
            contentType = "video/3gpp";  
        } else if (returnFileName.equals("mp4") ){  
            contentType = "video/mp4";  
        } else if (returnFileName.equals("mpeg") || returnFileName.equals("mpg") ){  
            contentType = "video/mpeg";  
        } else if (returnFileName.equals("mov")){  
            contentType = "video/quicktime";  
        } else if (returnFileName.equals("flv")){  
            contentType = "video/x-flv";  
        } else if (returnFileName.equals("m4v")){  
            contentType = "video/x-m4v";  
        } else if (returnFileName.equals("mng")){  
            contentType = "video/x-mng";  
        } else if (returnFileName.equals("asx") || returnFileName.equals("asf")){  
            contentType = "video/x-ms-asf";  
        } else if (returnFileName.equals("wmv")){  
            contentType = "video/x-ms-wmv";  
        } else if (returnFileName.equals("avi")){  
            contentType = "video/x-msvideo";  
        } else if (returnFileName.equals("apk")){
        	contentType = "application/vnd.android";
        }
          
        return contentType;  
    } 
	
	/**
	 * 获取ip地址
	 * @param request HttpServletRequest
	 * @return ipAddress String
	 */
	public static String getIpAddr(HttpServletRequest request){
		String ipAddress = null;
		ipAddress = request.getRemoteAddr();
		ipAddress = request.getHeader("x-forwarded-for");
		if(ipAddress == null || ipAddress.length() == 0 || "unknow".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getHeader("Proxy-Client-IP");
		}
		if(ipAddress == null || ipAddress.length() == 0 || "unknow".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getHeader("WL-Proxy-Client-IP");
		}
		if(ipAddress == null || ipAddress.length() == 0 || "unknow".equalsIgnoreCase(ipAddress)){
			ipAddress = request.getRemoteAddr();
			if(ipAddress.equals("127.0.0.1")){
				//根据网卡获取本机配置的IP
				InetAddress inet = null;
				try {
					inet = InetAddress.getLocalHost();
					ipAddress = inet.getHostAddress();
				} catch (UnknownHostException e) {
					LOG.error("获取 IP 地址失败",e);
					return null;
				}
			}
		}
		
		//通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照","分隔
		if(null != ipAddress && ipAddress.length() > 15){
			if(ipAddress.indexOf(",")>0){
				ipAddress = ipAddress.substring(0,ipAddress.indexOf(","));
			}
		}
		return ipAddress;
	}
	
	/**
	 * 获取端口号
	 * @param request
	 * @return
	 */
	public static String getPort(HttpServletRequest request){
		String port;
		port = String.valueOf(request.getRemotePort());
		return port;
	}
	
	
//	public static void main(String[] args){
//		String ip = "92.168.11.1";
//		String ips = "192.168.11.*";
//		String sub = ip.substring(0, ip.lastIndexOf("."));
//		System.out.println(ips.contains(sub));
//	}
	
}
